package com.settlement.system.controller;

import com.settlement.system.csv.CsvExportBuilder;
import com.settlement.system.csv.CsvExportUtil;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpReceiveRefundDetail;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpReceiveRefundDetailService;
import com.settlement.system.service.csv.DateSpiltCsvDatas;
import com.settlement.system.common.util.common.FasUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * AP_702收退货明细报表（公测期仅供参考） 前端控制器
 * @author Ximi
 * @since 2020-10-19
 */

@Api(tags = "AP_702收退货明细报表（公测期仅供参考）",description = "AP_702收退货明细报表（公测期仅供参考）")
@RestController
@RequestMapping("/wp/tblWpReceiveRefundDetail")
public class TblWpReceiveRefundDetailController  extends MergeApiExcelController<TblWpReceiveRefundDetail, Map<String, Object>> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpReceiveRefundDetailService tblWpReceiveRefundDetailService;

    @Override
    public IBaseService init() {
        return tblWpReceiveRefundDetailService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpReceiveRefundDetail.class;
    }


    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpReceiveRefundDetail foot = tblWpReceiveRefundDetailService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "vendorCode", "processType","dataSource");
        FasUtil.toUpperCase(model, "poNo","skuNo","refundNo");
        super.setParamsModel(model);
    }

    @Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> params = buildParam(request);
        setParamsModel(params);
        Object fileName = "WP-收退货明细";
        String path = exportRootPath + System.currentTimeMillis() + "/";
        CsvExportUtil.createCsv()
                .headClass(getModelClass())
                .fileName(fileName.toString())
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .csvDatas(new DateSpiltCsvDatas(tblWpReceiveRefundDetailService, params))
                .export(request,response);
    }
}

